package com.hnevc.downloaddemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {


    Button mBtnGetText;
    //TextView mTvData;
    ImageView imageView;

    // 6.2.1 定义handler
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 101){
                //获取消息中的数据
                String fileName = (String) msg.obj;
                //将data作为文本框的内容显示
                File picFile = new File(getCacheDir(),fileName);
                Bitmap bitmap = BitmapFactory.decodeFile(picFile.getAbsolutePath());
                imageView.setImageBitmap(bitmap);
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnGetText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //创建线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //访问网络
                        try {
                            //1 构建URL对象url
                            URL url = new URL("http://www.hnevc.com/images/2.jpg");
                            //2 获取HttpURLConnection对象
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            //3 设置访问方式
                            conn.setRequestMethod("GET");
                            //4 设置连接超时 *
                            conn.setConnectTimeout(5000);
                            //5 获取返回的数据字节流
                            InputStream is = conn.getInputStream();
                            //6  处理数据
                            String fileName = "1.jpg";
                            File picFile = new File(getCacheDir(), fileName);
                            if(picFile.exists()){
                                picFile.delete();
                            }
                            FileOutputStream fos = new FileOutputStream(picFile);
                            byte[] buffer = new byte[1024];
                            int len=0;
                            while ((len=is.read(buffer))!=-1){
                                fos.write(buffer,0,len);
                            }
                            fos.close();
                            //发送消息
                            Message msg = new Message();
                            msg.what=101;
                            msg.obj = fileName;
                            handler.sendMessage(msg);

                            //7 关闭连接
                            conn.disconnect();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }


    private void initView() {
        mBtnGetText = (Button) findViewById(R.id.id_btn_get_text);
        imageView = (ImageView) findViewById(R.id.id_iv_pic);
    }
}
